package org.jeecg.modules.cdkey.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.AnswerOnlineController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.cdkey.constant.CdkeyConstant;
import org.jeecg.modules.cdkey.entity.AoCdkey;
import org.jeecg.modules.cdkey.service.IAoCdkeyService;
import org.jeecg.modules.cdkey.vo.BatchAddCdkeyModel;
import org.jeecg.modules.cdkey.vo.BatchAssignCdkeyModel;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * 兑换码
 *
 * @author answer-online
 * @version V1.0
 * @date 2022-01-14
 */
@SuppressWarnings("unused")
@Api(tags = "兑换码")
@RestController
@RequestMapping("/cdkey/aoCdkey")
@Slf4j
public class AoCdkeyController extends AnswerOnlineController<AoCdkey, IAoCdkeyService> {

    @Resource
    private IAoCdkeyService aoCdkeyService;

    /**
     * 分页列表查询
     *
     * @param aoCdkey  param
     * @param pageNo   pageNo
     * @param pageSize pageSize
     * @param req      req
     * @return r
     */
    @AutoLog(value = "兑换码-分页列表查询")
    @ApiOperation(value = "兑换码-分页列表查询", notes = "兑换码-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(AoCdkey aoCdkey,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<AoCdkey> queryWrapper = QueryGenerator.initQueryWrapper(aoCdkey, req.getParameterMap());
        Page<AoCdkey> page = new Page<>(pageNo, pageSize);
        IPage<AoCdkey> pageList = aoCdkeyService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 批量添加
     *
     * @param batchAddCdkeyModel param
     * @return r
     */
    @AutoLog(value = "兑换码-批量生成")
    @ApiOperation(value = "兑换码-批量生成", notes = "兑换码-批量生成")
    @PostMapping(value = "/batchAdd")
    public Result<?> batchAdd(@RequestBody BatchAddCdkeyModel batchAddCdkeyModel) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        try {
            List<AoCdkey> cdkeyList = new ArrayList<>();
            for (int i = 1; i <= batchAddCdkeyModel.getNum(); i++) {
                AoCdkey cdkey = new AoCdkey();
                cdkey.setCode(UUID.randomUUID().toString().replaceAll("-", ""));
                cdkey.setCategoryId(batchAddCdkeyModel.getCategoryId());
                if (StringUtils.isNotEmpty(batchAddCdkeyModel.getOwner())) {
                    cdkey.setOwner(batchAddCdkeyModel.getOwner());
                    cdkey.setStatus(CdkeyConstant.WAIT_ACTIVE);
                }
                cdkeyList.add(cdkey);
            }
            aoCdkeyService.saveBatch(cdkeyList);
            return Result.OK("生成成功！");
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 通过id删除
     *
     * @param id id
     * @return r
     */
    @AutoLog(value = "兑换码-通过id删除")
    @ApiOperation(value = "兑换码-通过id删除", notes = "兑换码-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id") String id) {
        aoCdkeyService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return r
     */
    @AutoLog(value = "兑换码-批量删除")
    @ApiOperation(value = "兑换码-批量删除", notes = "兑换码-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids") String ids) {
        this.aoCdkeyService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 批量分配
     *
     * @param batchAssignCdkeyModel batchAssignCdkeyModel
     * @return r
     */
    @AutoLog(value = "兑换码-批量分配")
    @ApiOperation(value = "兑换码-批量分配", notes = "兑换码-批量分配")
    @PostMapping(value = "/assignBatch")
    public Result<?> assignBatch(@RequestBody BatchAssignCdkeyModel batchAssignCdkeyModel) {
        this.aoCdkeyService.update(new LambdaUpdateWrapper<AoCdkey>().set(AoCdkey::getOwner, batchAssignCdkeyModel.getOwner()).in(AoCdkey::getId, Arrays.asList(batchAssignCdkeyModel.getIds().split(","))));
        return Result.OK("分配成功!");
    }

    /**
     * 通过id查询
     *
     * @param id id
     * @return r
     */
    @AutoLog(value = "兑换码-通过id查询")
    @ApiOperation(value = "兑换码-通过id查询", notes = "兑换码-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id") String id) {
        AoCdkey aoCdkey = aoCdkeyService.getById(id);
        if (aoCdkey == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(aoCdkey);
    }

    /**
     * 导出excel
     *
     * @param request r
     * @param aoCdkey exl
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AoCdkey aoCdkey) {
        return super.exportXls(request, aoCdkey, AoCdkey.class, "兑换码");
    }

    /**
     * 查询近七天Cdkey数据（时间分组）
     *
     * @return r
     */
    @AutoLog(value = "题目表-查询近七天Cdkey数据（时间分组）")
    @ApiOperation(value = "题目表-查询近七天Cdkey数据（时间分组）", notes = "题目表-查询近七天Cdkey数据（时间分组）")
    @GetMapping(value = "/limit7groupByCreateTime")
    public Result<?> limit7groupByCreateTime() {
        return Result.OK(aoCdkeyService.limit7groupByCreateTime());
    }

    /**
     * 查询近30天Cdkey数据（时间分组）
     *
     * @return r
     */
    @AutoLog(value = "题目表-查询近30天Cdkey数据（时间分组）")
    @ApiOperation(value = "题目表-查询近30天Cdkey数据（时间分组）", notes = "题目表-查询近30天Cdkey数据（时间分组）")
    @GetMapping(value = "/limit30groupByCreateTime")
    public Result<?> limit30groupByCreateTime() {
        return Result.OK(aoCdkeyService.limit30groupByCreateTime());
    }

    /**
     * 查询近年Cdkey数据（时间分组）
     *
     * @return r
     */
    @AutoLog(value = "题目表-查询近年Cdkey数据（时间分组）")
    @ApiOperation(value = "题目表-查询近年Cdkey数据（时间分组）", notes = "题目表-查询近年Cdkey数据（时间分组）")
    @GetMapping(value = "/limitYearGroupByCreateTime")
    public Result<?> limitYearGroupByCreateTime() {
        return Result.OK(aoCdkeyService.limitYearGroupByCreateTime());
    }

}
